/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tordroid.ui;

import android.app.Fragment;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.style.StyleSpan;
import android.view.*;
import android.widget.*;
import de.tordroid.R;
import de.tordroid.core.Database;
import de.tordroid.core.Engine;
import de.tordroid.model.Story;

/**
 *
 * @author Christian Simon
 */
public class StoryboardFragment extends Fragment {

    private EditText edit_titel;
    private TextView text_addDate;
    private EditText edit_story;
    //private EditText edit_ingameTime;
    private NumberPicker np_ingameTime;
    private ToggleButton tg_ingameTime;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        Database.save();
    }

    public static StoryboardFragment newInstance() {
        StoryboardFragment fragment = new StoryboardFragment();
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUI();
        updateContent();
        initListener();

    }

    private void initUI() {
        edit_story = (EditText) getActivity().findViewById(R.id.storyboard_fragment_story);
        edit_titel = (EditText) getActivity().findViewById(R.id.storyboard_fragment_titel);
        text_addDate = (TextView) getActivity().findViewById(R.id.storyboard_fragment_datetime);
        np_ingameTime = (NumberPicker) getActivity().findViewById(R.id.storyboard_fragment_ingametime);
        tg_ingameTime = (ToggleButton) getActivity().findViewById(R.id.storyboard_fragment_ingametime_toogle);
    }

    private void updateContent() {
        edit_titel.setText(Engine.getStoryTitel());
        edit_story.setText(Html.fromHtml(Engine.getStoryContent()));
        text_addDate.setText(Engine.getStoryDate());
        np_ingameTime.setMinValue(0);
        np_ingameTime.setMaxValue(50);
        np_ingameTime.setValue(Engine.getStoryTime());
        tg_ingameTime.setChecked(Engine.getStoryIsAtc());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.storyboard_fragment, container, false);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.story, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_story_add:
                Engine.addCharacterStory(new Story());
                return true;
            case R.id.menu_story_delete:
                Engine.removeStory();
                return true;
            case R.id.menu_story_save:
                Database.save();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initListener() {
        np_ingameTime.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int time = np_ingameTime.getValue();
                Engine.setStoryTime(time);
            }
        });

        tg_ingameTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Engine.setStoryIsAtc(tg_ingameTime.isChecked());
            }
        });
        
        edit_story.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Engine.setStoryContent(Html.toHtml(edit_story.getText()));
            }
        });

        edit_story.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//                menu.clear();
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.text_edit, menu);
                return true;
            }

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                Spannable str = edit_story.getText();
                int start = edit_story.getSelectionStart();
                int end = edit_story.getSelectionEnd();
                if (str != null) {
                    switch (item.getItemId()) {
                        case R.id.menu_font_bold:
                            str.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);
                            break;
                        case R.id.menu_font_italic:
                            str.setSpan(new StyleSpan(Typeface.ITALIC), start, end, 0);
                            break;
                        case R.id.menu_font_normal:
                            str.setSpan(new StyleSpan(Typeface.NORMAL), start, end, 0);
                            break;
                        default:
                            return false;
                    }
                    edit_story.setText(str);
                }
                return true;
            }

            public void onDestroyActionMode(ActionMode mode) {
            }
        });

        edit_titel.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            public void afterTextChanged(Editable s) {
                Engine.setStoryTitel(edit_titel.getText().toString());
            }
        });
    }
}
